package com.wangjiang.rest;

import com.wangjiang.rest.entity.Result;
import com.wangjiang.rest.entity.ResultCodeEnum;

public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_INTERNAL_ERROR_MESSAGE = "INTERNAL_ERROR";

    //200
    public static <T> Result<T> genSuccessResult(T data) {
        return new Result()
                .setCode(ResultCodeEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }

    // 400
    public static Result genFailResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.FAIL.getCode())
                .setMessage(message);
    }

    // 404
    public static Result genNoFoundResult(String message) {
        return new Result()
                .setCode(ResultCodeEnum.NOT_FOUND.getCode())
                .setMessage(message);
    }

    // 500
    public static Result genInternalErrorResult(String message) {
        if(message.equals("")) message = DEFAULT_INTERNAL_ERROR_MESSAGE;

        return new Result()
                .setCode(ResultCodeEnum.INTERNAL_SERVER_ERROR.getCode())
                .setMessage(message);
    }

    //401
    public static Result genUnauthorizedResult() {
        return new Result()
                .setCode(ResultCodeEnum.UNAUTHORIZED.getCode())
                .setMessage("权限不足！");
    }
}
